<?php


namespace app\common\model;


use think\facade\Db;
use think\Model;

class MemberInfo extends Model
{
    public function setMemberTimeAttr($value)
    {
        return strtotime($value);
    }
    public function getMemberTimeAttr($value)
    {
        if(empty($value)){
            return '';
        }
        return date('Y-m-d',$value);
    }
    public function getMemberInfoById($where){
        $result = $this->where($where)->find();
        return $result;
    }

    public function getMemberInfoByIdField($where,$field){
        $result = $this->where($where)->field($field)->find();
        return $result;
    }

    public function changeMemberInfoFiled($where,$data){
        $result = $this->where($where)->find()->save($data);
        return $result;
    }
    public function getMemberPageWhere($where,$paging,$title,$address){
        $num = null;

        if (intval($paging['num'])>0){
            $num = $paging['num'];
        }else{
            $num = 10;
        }
        $current_page = $paging['current_page'];
        $count = $this->where($where)
            ->where('name','like','%'.$title.'%')
            ->where('address','like','%'.$address.'%')
            ->count();
        if ($count<1){
            return [];
        }
        $total = ceil($count/$num);
        $res = $this->where($where)
            ->where('name','like','%'.$title.'%')
            ->where('address','like','%'.$address.'%')
            ->order('id', 'desc')
            ->limit(($current_page-1)*$num,$num)
            ->select();

        $pageInfo = [
            "current_page"=>$current_page,
            "total"=>$total
        ];
        $result = [
            'pageInfo' =>$pageInfo,
            'list' => $res
        ];
        return $result;
    }

    public function getMemberPageBySupplementWhere($where,$paging,$title,$address,$trade_name){
        $num = null;

        if (intval($paging['num'])>0){
            $num = $paging['num'];
        }else{
            $num = 10;
        }
        $current_page = $paging['current_page'];
        if($where['member_category'] == 1){
            $count =Db::table('member_info')
                ->alias('a')
                ->join(['consignor_replenish'=>'b'],'a.member_id = b.member_id')
                ->where('a.name','like','%'.$title.'%')
                ->where('a.address','like','%'.$address.'%')
                ->where(['a.member_category'=>$where['member_category'],'a.status'=>1])
                ->where('b.trade_name','like','%'.$trade_name.'%')

                ->count();

//            $count = $this->where($where)
//                ->where('name','like','%'.$title.'%')
//                ->where('address','like','%'.$address.'%')
//                ->count();
            if ($count<1){
                return [];
            }
            $total = ceil($count/$num);
            $res = Db::table('member_info')
                ->alias('a')
                ->join(['consignor_replenish'=>'b'],'a.member_id=b.member_id')
                ->where('a.name','like','%'.$title.'%')
                ->where('a.address','like','%'.$address.'%')
                ->where(['a.member_category'=>$where['member_category'],'a.status'=>1])
                ->where('b.trade_name','like','%'.$trade_name.'%')
                ->order('a.id', 'desc')
                ->limit(($current_page-1)*$num,$num)
                ->select();

        }else{
            $count = Db::table('member_info')
                ->alias('a')
                ->join(['logistics_replenish'=>'b'],'a.member_id = b.member_id')
                ->where('a.name','like','%'.$title.'%')
                ->where('a.address','like','%'.$address.'%')
                ->where(['a.member_category'=>$where['member_category'],'a.status'=>1])
                ->where('b.trade_name','like','%'.$trade_name.'%')
                ->count();

            if ($count<1){
                return [];
            }
            $total = ceil($count/$num);
            $res = Db::table('member_info')
                ->alias('a')
                ->join(['logistics_replenish'=>'b'],'a.member_id=b.member_id')
                ->where('a.name','like','%'.$title.'%')
                ->where('a.address','like','%'.$address.'%')
                ->where(['a.member_category'=>$where['member_category'],'a.status'=>1])
                ->where('b.trade_name','like','%'.$trade_name.'%')
                ->order('a.id', 'desc')
                ->limit(($current_page-1)*$num,$num)
                ->select();
//                 dump($res);exit();
        }

        $pageInfo = [
            "current_page"=>$current_page,
            "total"=>$total
        ];
        $result = [
            'pageInfo' =>$pageInfo,
            'list' => $res
        ];
        return $result;
    }

    /**
     * 获取会员的所有字段
     * @param $id
     * @return array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDetailById($id){
        $result = $this->find($id);
        return $result;
    }

    /**
     * 获取会员的部分字段的会员信息
     * @param $id
     * @return array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDetailFieldsById($id){
        $result = $this->withoutField('contacts,position,mobile,landine_phone,email,wechat')->find($id);
        return $result;
    }

    public function getDetailFieldsByWhere($where){
        $result = $this->withoutField('contacts,position,mobile,landine_phone,email,wechat')->where($where)->find();
        return $result;
    }

    public function getMemberLimitList($where,$num){
        $result = $this->where($where)->order('id','desc')->field('name,id,member_id,logo,id')->limit($num)->select();
        return $result;
    }

    /**
     * 根据条件获取总数
     * @param $where
     * @return int
     */
    public function getCountByWhere($where){
        $res = $this->where($where)->count();
        return $res;
    }
}